package com.java01.apache.commons.collections;

import org.apache.commons.collections.BidiMap;
import org.apache.commons.collections.MapIterator;
import org.apache.commons.collections.bidimap.TreeBidiMap;

public class BiDirectionalMap {

	public static void main(String[] args) {
		BidiMap bidi = new TreeBidiMap();
		bidi.put("A", "1");
		bidi.put("B", "2");
		bidi.put("C", "3");
		bidi.put("D", "4");
		bidi.put("E", "4");
		
		System.out.println("1. "+bidi.get("A")); // get value by key
		System.out.println("2. "+bidi.getKey("2")); // get key by value
		System.out.println("3. "+bidi.removeValue("4")); // remove all whose value is "4"
		
		dumpMap(bidi);
		
		BidiMap inverse = bidi.inverseBidiMap();

		dumpMap(inverse);
	}
	
	public static void dumpMap(BidiMap bidi){
		System.out.println();
		
		MapIterator it=bidi.mapIterator();
		while(it.hasNext()){
			Object key=it.next();
			Object value=it.getValue();
			System.out.println(key+"->"+value);
		}
	}
}
